package com.yx.cloud.map.layer;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.view.MotionEvent;

import com.aaa.lib.map.MapUtils;
import com.aaa.lib.map.MapView;
import com.iot.product.sweeper.bean.ObjectRecognition;
import com.iot.common.utils.LogUtils;

/**
 * 标记点
 */
public class YXObjectRecognitionLayer extends YXImageMarkerLayer {

    public ObjectRecognition objectRecognition;
    private float[] tmpPoint = new float[2];
    private boolean showDetail = false;
    private Paint detailBgPaint;
    private Paint textPaint;
    private Paint aiFramePaint;
    private Path jianjian;
    private Bitmap icon;
    private float density;
    private PointF screenPoint;

    public YXObjectRecognitionLayer(MapView mapView, ObjectRecognition objectRecognition) {
        super(mapView, objectRecognition.getIcon(), new PointF(objectRecognition.getX(), objectRecognition.getY()));
        setLayerLevel(7);
        this.rotation = objectRecognition.getRotation();
        this.objectRecognition = objectRecognition;

        this.density = mapView.getResources().getDisplayMetrics().density;

        detailBgPaint = new Paint();
        detailBgPaint.setStyle(Paint.Style.FILL);
        detailBgPaint.setColor(Color.parseColor("#515151"));
        detailBgPaint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(10 * density);

        aiFramePaint = new Paint();
        aiFramePaint.setStyle(Paint.Style.STROKE);
        detailBgPaint.setAntiAlias(true);
        aiFramePaint.setColor(Color.WHITE);

        jianjian = new Path();
        screenPoint = new PointF();
    }

    public static boolean isPointInCircle(double x, double y, double cx, double cy, float radius) {
        double distanceX = Math.abs(x - (double) cx);
        double distanceY = Math.abs(y - (double) cy);
        int distanceZ = (int) Math.sqrt(Math.pow(distanceX, 2.0D) + Math.pow(distanceY, 2.0D));
        return (float) distanceZ <= radius;
    }

    @Override
    public boolean onTouch(MotionEvent event) {

        float touchX = event.getX();
        float touchY = event.getY();
        float iconHeight = markerBitmap.getHeight();

        tmpPoint = new float[]{markerPosition.x, markerPosition.y};
        mMapView.getTransform().mapPoints(tmpPoint);
        if (isPointInCircle(touchX, touchY, tmpPoint[0], tmpPoint[1] - iconHeight / 2, markerBitmap.getWidth())) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                showDetail = !showDetail;
                mMapView.refresh();
            }
            return true;
        }
        return false;
    }

    @Override
    public void draw(Canvas canvas) {
        if (!visible) {
            return;
        }
        if (markerBitmap == null || markerBitmap.isRecycled() || icon == null || icon.isRecycled()) {
            return;
        }
        if (markerPosition == null || (markerPosition.x == 0 && markerPosition.y == 0)) {
            return;
        }

        LogUtils.i("YXImageMarkerLayer  x : " + markerPosition.x + " y : " + markerPosition.y);
        //这种物体识别的图标以九宫格中的8的位置放置
        float iconRadiusWidth = markerBitmap.getWidth();
        float iconRadiusHeight = markerBitmap.getHeight();

        MapUtils.getTransformedPoint(mMapView.getTransform(), markerPosition.x, markerPosition.y, screenPoint);
        PointF pointF = screenPoint;

        canvas.drawBitmap(markerBitmap, pointF.x - iconRadiusWidth / 2, pointF.y - iconRadiusHeight, null);

        //绘制弹窗
        if (showDetail) {
            Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
            float textHeight = fontMetrics.descent - fontMetrics.ascent;    //计算文本高度
            float baselineOffsetCenter = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.descent;

            float iconHeight = icon.getHeight();    //图标大小
            float iconWidth = icon.getWidth();      //图标大小

            float bottomX = pointF.x;               //弹窗气泡尖尖坐标
            float bottomY = pointF.y - markerBitmap.getHeight();
            float innerPadding = 7 * density;                             //统一的间距
            float triangle = 7 * density;                            //气泡尖尖高度
            float bgBottom = bottomY - triangle;            //气泡底部Y
            float nameBottom = bgBottom - innerPadding;          // 名称文字底部
            float iconBottom = nameBottom - textHeight - innerPadding;   //图标底部
            float textBottom = iconBottom - iconHeight - innerPadding;   //图标上方文字底部
            float bgTop = textBottom - textHeight - innerPadding;        //气泡顶部

            float nameTextWidth = textPaint.measureText(objectRecognition.getName());
            float similarytyTextWidth = textPaint.measureText(objectRecognition.getSimilarity() + "%");
            float aiTextWidth = textPaint.measureText("AI");

            float centerSimilarytyX = pointF.x - iconWidth / 2 - similarytyTextWidth / 2;
            float aiFramePadding = 2.5f * density;
            float aiFrameRight = centerSimilarytyX - similarytyTextWidth / 2 - innerPadding;
            float leftBg = aiFrameRight - aiTextWidth - aiFramePadding * 2 - innerPadding;
            float rightBg = leftBg + (pointF.x - leftBg) * 2;

            jianjian.reset();
            jianjian.moveTo(bottomX, bottomY);
            jianjian.lineTo(bottomX - 10, bgBottom - 1);
            jianjian.lineTo(bottomX + 10, bgBottom - 1);
            jianjian.close();
            canvas.drawPath(jianjian, detailBgPaint);

            canvas.drawRoundRect(leftBg, bgTop, rightBg, bgBottom, 10 * density, 10 * density, detailBgPaint);
            canvas.drawText(objectRecognition.getSimilarity() + "%", centerSimilarytyX - similarytyTextWidth / 2, textBottom - textHeight / 2 + baselineOffsetCenter, textPaint);

            canvas.drawRoundRect(aiFrameRight - aiTextWidth - aiFramePadding * 2, textBottom - textHeight, aiFrameRight, textBottom, aiFramePadding, aiFramePadding, aiFramePaint);
            canvas.drawText("AI", aiFrameRight - aiTextWidth - aiFramePadding, textBottom - textHeight / 2 + baselineOffsetCenter, textPaint);

            canvas.drawText("已避开", pointF.x + iconWidth / 2, textBottom - textHeight / 2 + baselineOffsetCenter, textPaint);
            canvas.drawBitmap(icon, pointF.x - iconWidth / 2, iconBottom - iconHeight, null);

            canvas.drawText(objectRecognition.getName(), pointF.x - nameTextWidth / 2, nameBottom - textHeight / 2 + baselineOffsetCenter, textPaint);
        }
    }

    public ObjectRecognition getObjectRecognition() {
        return objectRecognition;
    }

    public boolean isShowDetail() {
        return showDetail;
    }

    public void setShowDetail(boolean showDetail) {
        this.showDetail = showDetail;
    }

    public void setIcon(Bitmap icon) {
        this.icon = icon;
    }

    @Override
    public void release() {
        if (markerBitmap != null && !markerBitmap.isRecycled()) {
            markerBitmap.recycle();
        }
        if (icon != null && !icon.isRecycled()) {
            icon.recycle();
        }
    }
}
